<?php

namespace yuankezhan\yiiTableWidget;

class TableToolCommand
{
    // 图标类型
    const TYPE_ADD = 1;        //  添加图标
    const TYPE_EDIT = 2;       //  修改图标
    const TYPE_DELETE = 3;     //  删除图标
    const TYPE_DOWNLOAD = 4;   //  下载图标
    const TYPE_UPLOAD = 5;     //  上传图标
    const TYPE_PRINT = 6;      //  打印图标
    const TYPE_SELF = 7;       //  自定义图标

    // 点击图标触发回调事件的类型
    const CALLBACK_TYPE_TARGET_HREF = 1;    // 新页面打开的a标签
    const CALLBACK_TYPE_HREF = 2;           // 不新页面打开的a标签
    const CALLBACK_TYPE_JS_FUNC = 3;        // JS的方法名

    /**
     * @var int $type 图标类型
     */
    public int $type;

    /**
     * @var string $callback 点击图标触发
     */
    public string $callback;

    /**
     * @var int $callbackType 点击图标触发回调事件的类型
     */
    public int $callbackType;

    /**
     * @var string $title 鼠标悬停在图标上显示的标题
     */
    public string $title;

    /**
     * @var string $selfIcon 自定义操作显示的图标，支持img路径和<svg>标签
     */
    public string $selfIcon;

    public array $iconConfigArray = [
        self::TYPE_ADD => '<svg t="1712734892055" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1780" width="200" height="200"><path d="M820.1 487.1H537.2V204.2c0-13.8-11.2-25-25-25s-25 11.2-25 25v282.9H204.4c-13.8 0-25 11.2-25 25s11.2 25 25 25h282.9V820c0 13.8 11.2 25 25 25s25-11.2 25-25V537.1h282.9c13.8 0 25-11.2 25-25-0.1-13.8-11.3-25-25.1-25z" fill="#424242" p-id="1781"></path></svg>',
        self::TYPE_EDIT => '<svg t="1712734850471" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1627" width="200" height="200"><path d="M894.6 892.8H123.3c-13.8 0-25 11.2-25 25s11.2 25 25 25h771.4c13.8 0 25-11.2 25-25s-11.3-25-25.1-25zM399.8 811c0 13.8 11.2 25 25 25h462.7c13.8 0 25-11.2 25-25s-11.2-25-25-25H424.8c-13.8 0-25 11.2-25 25zM146.3 841.3c4.2 2.9 9.1 4.3 14 4.3 4.4 0 8.8-1.2 12.7-3.5L335.8 746c4.9-1.7 9.1-5 12.1-9.3l307.5-442.5 0.9-1.2 57.5-82.7c7.9-11.3 5.1-26.9-6.3-34.8l-136.7-95c-5.4-3.8-12.2-5.3-18.7-4.1-6.5 1.2-12.3 4.9-16.1 10.3L481.6 165l-0.9 1.2-310.6 447c-1.2 1.8-2.2 3.7-2.9 5.7-1 2.2-1.8 4.5-2.2 7l-29.4 190.9c-1.5 9.5 2.7 19.1 10.7 24.5z m46.7-69.1l15.4-99.8 71.8 48.4-87.2 51.4z m369.8-636.3l95.6 66.5-26.8 38.5-95.6-66.5 26.8-38.5z m-57.5 82.7l95.6 66.5-279.8 402.6-95.6-66.5 279.8-402.6z" fill="#424242" p-id="1628"></path></svg>',
        self::TYPE_DELETE => '<svg t="1712734908103" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1933" width="200" height="200"><path d="M392.3 301.8c-13.8 0-25 11.2-25 25V759c0 13.8 11.2 25 25 25s25-11.2 25-25V326.8c0-13.8-11.2-25-25-25zM631.4 301.8c-13.8 0-25 11.2-25 25V759c0 13.8 11.2 25 25 25s25-11.2 25-25V326.8c0-13.8-11.2-25-25-25z" fill="#424242" p-id="1934"></path><path d="M935.6 193.2h-202v-6.8c0-68.9-56.1-125-125-125H422.3c-68.9 0-125 56.1-125 125v6.8H89.5c-13.8 0-25 11.2-25 25s11.2 25 25 25h69.3V797c0 90.8 75.1 164.8 167.3 164.8h375c92.3 0 167.3-73.9 167.3-164.8V243.2h67.1c13.8 0 25-11.2 25-25s-11.1-25-24.9-25z m-588.3-6.8c0-41.4 33.6-75 75-75h186.3c41.4 0 75 33.6 75 75v6.8H347.3v-6.8zM818.5 797c0 63.3-52.6 114.8-117.3 114.8h-375c-64.7 0-117.3-51.5-117.3-114.8V243.2h609.8V797z" fill="#424242" p-id="1935"></path></svg>',
        self::TYPE_DOWNLOAD => '<svg t="1712734946207" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2087" width="200" height="200"><path d="M938.2 671.3c-13.8 0-25 11.2-25 25v83.1c0 63.3-52.6 114.7-117.3 114.7H230.2c-64.7 0-117.3-51.5-117.3-114.7v-83.1c0-13.8-11.2-25-25-25s-25 11.2-25 25v83.1c0 90.8 75.1 164.7 167.3 164.7h565.7c92.3 0 167.3-73.9 167.3-164.7v-83.1c0-13.8-11.2-25-25-25z" fill="#424242" p-id="2088"></path><path d="M494.4 692.4c4.7 4.7 11.1 7.4 17.7 7.4h0.1c6.7 0 13-2.7 17.7-7.4l189.7-192c9.7-9.8 9.6-25.7-0.2-35.4-9.8-9.7-25.7-9.6-35.4 0.2l-145.3 147V98.5c0-13.8-11.2-25-25-25s-25 11.2-25 25v517.4L338.5 465.1c-9.7-9.8-25.6-9.8-35.4-0.1-9.8 9.7-9.8 25.6-0.1 35.4l191.4 192z" fill="#424242" p-id="2089"></path></svg>',
        self::TYPE_UPLOAD => '<svg t="1712735064904" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2241" width="200" height="200"><path d="M938.2 671.3c-13.8 0-25 11.2-25 25v83.1c0 63.3-52.6 114.7-117.3 114.7H230.2c-64.7 0-117.3-51.5-117.3-114.7v-83.1c0-13.8-11.2-25-25-25s-25 11.2-25 25v83.1c0 90.8 75.1 164.7 167.3 164.7h565.7c92.3 0 167.3-73.9 167.3-164.7v-83.1c0-13.8-11.2-25-25-25z" fill="#424242" p-id="2242"></path><path d="M494.4 80.8c4.7-4.7 11.1-7.4 17.7-7.4h0.1c6.7 0 13 2.7 17.7 7.4l189.7 192c9.7 9.8 9.6 25.7-0.2 35.4-9.8 9.7-25.7 9.6-35.4-0.2L538.8 161v513.7c0 13.8-11.2 25-25 25s-25-11.2-25-25V157.3L338.5 308.1c-9.7 9.8-25.6 9.8-35.4 0.1-9.8-9.7-9.8-25.6-0.1-35.4l191.4-192z" fill="#424242" p-id="2243"></path></svg>',
        self::TYPE_PRINT => '<svg t="1712727076644" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7853" width="200" height="200"><path d="M928 288H96v384h160v224h512V672h160zM704 832H320V544h384z m160-224h-96v-128H256v128h-96V352h704zM320 192h384v64h64V128H256v128h64v-64z" fill="#333333" p-id="7854"></path><path d="M384 608h256v64H384zM384 704h256v64H384z" fill="#333333" p-id="7855"></path><path d="M800 416m-32 0a32 32 0 1 0 64 0 32 32 0 1 0-64 0Z" fill="#333333" p-id="7856"></path></svg>',
    ];

    public function __construct(int $type, string $callback, int $callbackType, string $selfIcon, string $title)
    {
        $this->type = $type;
        $this->callback = $callback;
        $this->callbackType = $callbackType;
        $this->selfIcon = $selfIcon;
        $this->title = $title;

        if ($type === self::TYPE_SELF)
        {
            // 要检查的字符串
            $substring = substr($selfIcon, 0, 4);
            if ($substring == "<svg") {
                $this->iconConfigArray[self::TYPE_SELF] = $selfIcon;
            } else {
                $this->iconConfigArray[self::TYPE_SELF] = "<img src='{$selfIcon}'/>";
            }
        }
    }

    /**
     * 更新用户信息
     * @param int $type  图标类型
     * @param string $callback  点击图标触发
     * @param int $callbackType  点击图标触发回调事件的类型
     * @param string $selfIcon  自定义操作显示的图标，支持img路径和<svg>标签
     * @param string $title  鼠标悬停在图标上显示的标题
     * @return TableToolCommand
     */
    public static function set(int $type, string $callback, int $callbackType = TableToolCommand::CALLBACK_TYPE_TARGET_HREF, string $selfIcon = "", string $title = "")
    {
        return new static($type, $callback, $callbackType, $selfIcon, $title);
    }
}